<%@ include file="database_ini.jsp"%>


<%
	String dropid = request.getParameter("DROPID");
	request.setAttribute("DROPID",dropid);
	response.sendRedirect("index.jsp?DROPID=" +dropid);
	/*********debug*/
	System.out.println("DROPID: " + dropid);
	/**********/

	String LOC[] = request.getParameterValues("LOC");
	if (LOC != null) {
		for (int i = 0; i < LOC.length; i++) {
	out.println(LOC[i]);
	System.out.println("LOC: " + LOC[i]);
		}
	}
	
	/*********debug*/
	System.out.println("LOC: " + LOC);
	/**********/
	
	String SKU[] = request.getParameterValues("SKU");
	if (SKU != null) {
		for (int i = 0; i < SKU.length; i++) {
	out.println(SKU[i]);
	System.out.println("SKU: " + SKU[i]);
		}
	}
	String COUNTQTY_str[] = request.getParameterValues("COUNTQTY");
	int COUNTQTY_int[] = new int[COUNTQTY_str.length];
	if (COUNTQTY_str != null) {
		for (int i = 0; i < COUNTQTY_str.length; i++) {
	
	out.println(COUNTQTY_str[i]);
	COUNTQTY_int[i] = Integer.parseInt(COUNTQTY_str[i]);
	/*********debug*/
	System.out.println("COUNTQTY: " + COUNTQTY_str[i]);
	System.out.println("COUNTQTY_str.length: "
			+ COUNTQTY_str.length);
	System.out.println("COUNTQTY_int: " + COUNTQTY_int.length);
	/**********/
		}
	}
	
	System.out.println("COUNTQTY_int: " + COUNTQTY_int);
	
	for (int a = 0; a < LOC.length; a++) {

		int COUNTQTY_int_re = COUNTQTY_int[a];
		int COUNTQTY_int_re_temp = 0;
		int QTY_rs = 0;
		String LOT_rs ="";
		String ID_rs ="";
		Statement st = null;
		String sql = "select CCKEY, CCDROPID, LOC, SKU, DESCR, UPCCODE, LOTTBALOE05, CASECNT, DISPLAYSYSTEMCOUNT, Lot, ID, QTY from CCDET "
		+ "WHERE CCDROPID2 = ? and SKU = ? and LOC = ? order by CCKEY, CCDROPID, LOC, SKU, DESCR, UPCCODE, LOTTBALOE05, CASECNT, DISPLAYSYSTEMCOUNT, LOT DESC, ID DESC";

		pstmt = conn.prepareStatement(sql);
		pstmt.setString(1, dropid);
		pstmt.setString(2, SKU[a]);
		pstmt.setString(3, LOC[a]);

		ResultSet rs = pstmt.executeQuery();
		boolean firstrecord = true;
		while (rs.next() && COUNTQTY_int_re!=0) {
	//String CCKEY = rs.getString("CCKEY");
	//String LOC_rs = rs.getString("LOC");
	//String SKU_rs = rs.getString("SKU");
	
	if(!firstrecord){
	
		if(COUNTQTY_int_re > QTY_rs) {
			COUNTQTY_int_re_temp = QTY_rs;
			COUNTQTY_int_re = COUNTQTY_int_re - QTY_rs;
		}
		
		st = conn.createStatement();
		st.executeUpdate("update CCDET set COUNTQTY2 = "
				+ COUNTQTY_int_re_temp + " where SKU='" + SKU[a]
				+ "' and LOC = '" + LOC[a] + "' and LOT = '"
				+ LOT_rs + "' and ID = '" + ID_rs + "'");
	}
	firstrecord = false;
	
	
	
	LOT_rs = rs.getString("LOT");
	ID_rs = rs.getString("ID");
	QTY_rs = rs.getInt("QTY");
	
	/*********debug*/
	System.out.println("***************Round " + a);
	System.out.println("LOC_rs: " + LOC[a]);
	System.out.println("SKU_rs: " + SKU[a]);
	System.out.println("LOT_rs: " + LOT_rs);
	System.out.println("ID_rs: " + ID_rs);
	System.out.println("QTY_rs: " + QTY_rs);
	/**********/
	
	
	if (COUNTQTY_int_re <= QTY_rs) {
		
		/*********debug*/			
		System.out.println("COUNTQTY_int_re_temp: " + COUNTQTY_int_re_temp);
		System.out.println("COUNTQTY_int_re: " + COUNTQTY_int_re);
		/**********/
		COUNTQTY_int_re_temp = COUNTQTY_int_re;
		COUNTQTY_int_re = 0;
	}
	
	

	/*********debug*/			
	System.out.println("COUNTQTY_int_re_temp: " + COUNTQTY_int_re_temp);
	System.out.println("COUNTQTY_int_re: " + COUNTQTY_int_re);
	/**********/
	
		}
		
		if (COUNTQTY_int_re > QTY_rs) {
		COUNTQTY_int_re_temp = COUNTQTY_int_re;
		COUNTQTY_int_re = 0;		
		}
	
		st = conn.createStatement();
		st.executeUpdate("update CCDET set COUNTQTY2 = "
				+ COUNTQTY_int_re_temp + " where SKU='" + SKU[a]
				+ "' and LOC = '" + LOC[a] + "' and LOT = '" + LOT_rs
				+ "' and ID = '" + ID_rs + "'");
	}
	conn.close();
%>